home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / UNIXTOOL / MEMACS / C / Fileio < prev    next >
Text File  |  1990-06-26  |  6KB  |  291 lines

  1. /*  FILEIO.C:   Low level file i/o routines
  2.         MicroEMACS 3.10
  3.  
  4.  * The routines in this file read and write ASCII files from the disk. All of
  5.  * the knowledge about files are here.
  6.  */
  7.  
  8. #include        <stdio.h>
  9. #include    "estruct.h"
  10. #include    "eproto.h"
  11. #include        "edef.h"
  12. #include    "elang.h"
  13.  
  14. #if    AOSVS
  15. #define    fopen    xxfopen
  16. #endif
  17.  
  18. NOSHARE FILE *ffp;        /* File pointer, all functions. */
  19. static int eofflag;        /* end-of-file flag */
  20.  
  21. /*
  22.  * Open a file for reading.
  23.  */
  24. #if !(VMS & RMSIO)    /* if using RMS under VMS, the code */
  25.             /* is in VMS.C */
  26. PASCAL NEAR ffropen(fn)
  27. char    *fn;
  28. {
  29.         if ((ffp=fopen(fn, "r")) == NULL)
  30.                 return(FIOFNF);
  31.     eofflag = FALSE;
  32.         return(FIOSUC);
  33. }
  34.  
  35. /*
  36.  * Open a file for writing. Return TRUE if all is well, and FALSE on error
  37.  * (cannot create).
  38.  */
  39. #if    AOSVS == 0
  40. PASCAL NEAR ffwopen(fn, mode)
  41. char    *fn;
  42. char *mode;    /* mode to open file for */
  43. {
  44.     char xmode[6];        /* extended file open mode */
  45.  
  46.     /* nonstandard line terminators? */
  47.     if (*lterm) {
  48.  
  49.         /* open in binary mode */
  50.         strcpy(xmode, mode);
  51.         strcat(xmode, "b");
  52.         ffp = fopen(fn, xmode);
  53.     } else {
  54.  
  55.         /* open in ascii(text) mode */
  56.         ffp = fopen(fn, mode);
  57.     }
  58.  
  59.         if (ffp == NULL) {
  60.                 mlwrite(TEXT155);
  61. /*                      "Cannot open file for writing" */
  62.                 return(FIOERR);
  63.         }
  64.         return(FIOSUC);
  65. }
  66. #endif
  67.  
  68. /*
  69.  * Close a file. Should look at the status in all systems.
  70.  */
  71. PASCAL NEAR ffclose()
  72. {
  73.     /* free this since we do not need it anymore */
  74.     if (fline) {
  75.         free(fline);
  76.         fline = NULL;
  77.     }
  78.  
  79. #if    MSDOS & CTRLZ
  80.     putc(26, ffp);        /* add a ^Z at the end of the file */
  81. #endif
  82.     
  83. #if     RISCOS | V7 | USG | SMOS | HPUX | SUN | XENIX | BSD | WMCS | VMS | (MSDOS & (LATTICE | MSC | DTL | TURBO)) | OS2 | (ST520 & MWC) | AVIION
  84.         if (fclose(ffp) != FALSE) {
  85.                 mlwrite(TEXT156);
  86. /*                      "Error closing file" */
  87.                 return(FIOERR);
  88.         }
  89.         return(FIOSUC);
  90. #else
  91.         fclose(ffp);
  92.         return(FIOSUC);
  93. #endif
  94. }
  95.  
  96. /*
  97.  * Write a line to the already opened file. The "buf" points to the buffer,
  98.  * and the "nbuf" is its length, less the free newline. Return the status.
  99.  * Check only at the newline.
  100.  */
  101. PASCAL NEAR ffputline(buf, nbuf)
  102.  
  103. char    buf[];
  104. int nbuf;
  105.  
  106. {
  107.         register int i;        /* index into line to write */
  108.     register char *lptr;    /* ptr into the line terminator */
  109. #if    CRYPT
  110.     char c;        /* character to translate */
  111.  
  112.     if (cryptflag) {
  113.             for (i = 0; i < nbuf; ++i) {
  114.             c = buf[i];
  115.             crypt(&c, 1);
  116.             putc(c, ffp);
  117.         }
  118.     } else
  119.             for (i = 0; i < nbuf; ++i)
  120.                     putc(buf[i], ffp);
  121. #else
  122.         for (i = 0; i < nbuf; ++i)
  123.                 putc(buf[i], ffp);
  124. #endif
  125.  
  126.     /* write out the appropriate line terminator(s) */
  127.     if (*lterm) {
  128.         lptr = <erm[0];
  129.         while (*lptr)
  130.             putc(*lptr++, ffp);
  131.     } else {
  132.             putc('\n', ffp);
  133.     }
  134.  
  135.     /* check for write errors */
  136.         if (ferror(ffp)) {
  137.                 mlwrite(TEXT157);
  138. /*                      "Write I/O error" */
  139.                 return(FIOERR);
  140.         }
  141.  
  142.         return(FIOSUC);
  143. }
  144.  
  145. /*
  146.  * Read a line from a file, and store the bytes in the supplied buffer. The
  147.  * "nbuf" is the length of the buffer. Complain about long lines and lines
  148.  * at the end of the file that don't have a newline present. Check for I/O
  149.  * errors too. Return status.
  150.  */
  151. PASCAL NEAR ffgetline()
  152.  
  153. {
  154.         register int c;        /* current character read */
  155.         register int i;        /* current index into fline */
  156.     register char *tmpline;    /* temp storage for expanding line */
  157.     register char term;    /* Last character of lterm */
  158.     register int len;    /* Length of lterm */
  159.  
  160.     len = strlen(lterm);
  161.  
  162.     if (len == 0)
  163.         term = '\n';
  164.     else
  165.         term = lterm[len-1];
  166.  
  167.     /* if we are at the end...return it */
  168.     if (eofflag)
  169.         return(FIOEOF);
  170.  
  171.     /* dump fline if it ended up too big */
  172.     if (flen > NSTRING && fline != NULL) {
  173.         free(fline);
  174.         fline = NULL;
  175.     }
  176.  
  177.     /* if we don't have an fline, allocate one */
  178.     if (fline == NULL)
  179.         if ((fline = malloc(flen = NSTRING)) == NULL)
  180.             return(FIOMEM);
  181.  
  182.     /* read the line in */
  183.         i = 0;
  184.         while ((c = getc(ffp)) != EOF) {
  185.         fline[i++] = c;
  186.  
  187.         /* Check for the end of the line */
  188.         if (c == term)
  189.         {
  190.             if (len <= 1)
  191.             {
  192.                 --i;
  193.                 break;
  194.             }
  195.             else if (i >= len && !memcmp(lterm,&fline[i-len],len))
  196.             {
  197.                 i -= len;
  198.                 break;
  199.             }
  200.         }
  201.  
  202.         /* if it's longer, get more room */
  203.                 if (i >= flen) {
  204.             flen *= 2;
  205.             fline = realloc(fline, flen);
  206.                 }
  207.         }
  208.  
  209. #if 0
  210.     /* dump any extra line terminators at the end */
  211.     while (i > 0 && (fline[i-1] == 10 || fline[i-1] == 13))
  212.         i--;
  213.  
  214.     /* we should be ready to dump leading terminators too - ADD THIS DAN */
  215. #endif
  216.  
  217.     /* test for any errors that may have occured */
  218.         if (c == EOF) {
  219.                 if (ferror(ffp)) {
  220.                         mlwrite(TEXT158);
  221. /*                              "File read error" */
  222.                         return(FIOERR);
  223.                 }
  224.  
  225.                 if (i != 0)
  226.             eofflag = TRUE;
  227.         else
  228.             return(FIOEOF);
  229.         }
  230.  
  231.     /* terminate and decrypt the string */
  232.         fline[i] = 0;
  233. #if    CRYPT
  234.     if (cryptflag)
  235.         crypt(fline, strlen(fline));
  236. #endif
  237.         return(FIOSUC);
  238. }
  239. #endif
  240.  
  241. int PASCAL NEAR fexist(fname)    /* does <fname> exist on disk? */
  242.  
  243. char *fname;        /* file to check for existance */
  244.  
  245. {
  246.     FILE *fp;
  247.  
  248.     /* try to open the file for reading */
  249.     fp = fopen(fname, "r");
  250.  
  251.     /* if it fails, just return false! */
  252.     if (fp == NULL)
  253.         return(FALSE);
  254.  
  255.     /* otherwise, close it and report true */
  256.     fclose(fp);
  257.     return(TRUE);
  258. }
  259.  
  260. #if    AZTEC & MSDOS
  261. /*    a1getc:        Get an ascii char from the file input stream
  262.             but DO NOT strip the high bit
  263. */
  264.  
  265. #undef    getc
  266.  
  267. int a1getc(fp)
  268.  
  269. FILE *fp;
  270.  
  271. {
  272.     int c;        /* translated character */
  273.  
  274.     c = getc(fp);    /* get the character */
  275.  
  276.     /* if its a <LF> char, throw it out  */
  277.     while (c == 10)
  278.         c = getc(fp);
  279.  
  280.     /* if its a <RETURN> char, change it to a LF */
  281.     if (c == '\r')
  282.         c = '\n';
  283.  
  284.     /* if its a ^Z, its an EOF */
  285.     if (c == 26)
  286.         c = EOF;
  287.  
  288.     return(c);
  289. }
  290. #endif
  291.